package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.CuccCustInfoDTO;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 接入地
 */
public class AccessAddressAttrHandler extends AbstractAttrHandler {

    @Autowired
    IOperateSceneInstService operateSceneInstService;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        String custId = data.getCustId();
        CuccCustInfoDTO cuccCustInfoDTO = CuccPosServiceUtils.queryNewCustInfo(custId);
        String provinceName = cuccCustInfoDTO.getProvinceName();
        String sceneInstId = data.getSceneInstId();
        KiteOfferInst offerInst = querySceneInstService.getMainOfferInst(sceneInstId);
        String offerId = offerInst.getOfferId();
        List<AttrValue> kiteAttrValues = AttrValueCache.get(attr.getAttrId()).stream().filter(kiteAttrValue -> KiteStringUtils.isEqual(provinceName, kiteAttrValue.getAttrValueName())).collect(Collectors.toList());
        // 当产品是“行业5G消息”，此属性改为自动获取，且不可编辑
        if (KiteStringUtils.equalsAny(offerId, "10004322", "50040896") && KiteListUtils.isNotEmpty(kiteAttrValues)) {
            KiteAttrValue kiteAttrValue = KiteBeanUtils.transform(kiteAttrValues.get(0), KiteAttrValue.class);
            attr.setValue(kiteAttrValue.getAttrValue());
            attr.setValueDesc(kiteAttrValue.getAttrValueDesc());
            attr.setDefaultValue(kiteAttrValue.getAttrValue());
            operateSceneInstService.setAttr(sceneInstId, inst.getInstType(), inst.getInstId(), attr);
        }
    }

}
